package com.xique.door.biz.domain.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.CardDevice;
import com.xique.door.bean.domain.DCard;
import com.xique.door.bean.domain.estate.EstateUser;
import com.xique.door.bean.domain.estate.EstateUserBuilding;
import com.xique.door.bean.domain.estate.EstateUserCard;
import com.xique.door.bean.domain.estate.EstateUserDevice;
import com.xique.door.biz.domain.service.IEstateUserDomainService;
import com.xique.door.service.IDCardDeviceService;
import com.xique.door.service.IDCardService;
import com.xique.door.service.estate.IEstateUserBuildingService;
import com.xique.door.service.estate.IEstateUserCardService;
import com.xique.door.service.estate.IEstateUserDeviceService;
import com.xique.door.service.estate.IEstateUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author shd
 * @create 2022/6/9 15:38
 */
@Service
public class EstateUserDomainServiceImpl implements IEstateUserDomainService {

    @Autowired
    private IEstateUserService estateUserService;

    @Autowired
    private IDCardService cardService;

    @Autowired
    private IEstateUserCardService userCardService;

    @Autowired
    private IEstateUserBuildingService userBuildingService;

    @Autowired
    private IEstateUserDeviceService userDeviceService;

    @Autowired
    private IDCardDeviceService cardDeviceService;

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public AjaxResult addEstateUser(EstateUser user, DCard card, List<EstateUserBuilding> buildings, List<EstateUserDevice> devices, List<CardDevice> cardDevices) {

        int count = estateUserService.insert(user);
        if (count <= 0) {
            return AjaxResult.error("添加用户信息失败");
        }

        if (StringUtils.isNotNull(card)) {
            count = cardService.insertDCard(card);

            if (count <= 0) {
                return AjaxResult.error("添加卡片信息失败");
            }

            EstateUserCard userCard = new EstateUserCard();
            userCard.setUserId(user.getId());
            userCard.setCardId(card.getId());

            boolean isSuccess = userCardService.save(userCard);
            if (!isSuccess) {
                return AjaxResult.error("绑定用户跟卡片信息失败");
            }

            cardDevices.forEach(item -> {
                item.setCardId(card.getId());
            });

            count = cardDeviceService.batchInsertCardDevices(cardDevices);

            if (count <= 0) {
                return AjaxResult.error("绑定卡片跟设备信息失败");
            }
        }

        if (CollectionUtil.isNotEmpty(buildings)) {
            buildings.forEach(item -> {
                item.setUserId(user.getId());
            });
            boolean isSuccess = userBuildingService.saveBatch(buildings);
            if (!isSuccess) {
                return AjaxResult.error("绑定用户跟楼栋信息失败");
            }
        }

        if (CollectionUtil.isNotEmpty(devices)) {
            devices.forEach(item -> {
                item.setUserId(user.getId());
                item.setCustomId(user.getCustomId());
            });
            boolean isSuccess = userDeviceService.saveBatch(devices);
            if (!isSuccess) {
                return AjaxResult.error("绑定用户跟设备信息失败");
            }
        }

        return AjaxResult.success();
    }
}
